package com.guangzhi.weijianzhi.activity;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.guangzhi.weijianzhi.R;
import com.guangzhi.weijianzhi.http.HttpRequestUtils;
import com.guangzhi.weijianzhi.http.MyAsyncHttpResponseHandler;
import com.guangzhi.weijianzhi.utils.Misc;

public class PayPassWordActivity extends BaseActivity implements OnClickListener {

	private TextView mTime;
	private TimeCount timeCount;
	private EditText mLodPass;
	private EditText mNewPass;
	private EditText mNewPassTwo,mPassCode;
	private String mCode;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tz_pay_pass_layout);
		timeCount = new TimeCount(120000, 1000);
		initView();
		
	}

	private void initView() {
		// TODO Auto-generated method stub
		initTitle();
		goBack.setOnClickListener(this);
		titleTextV.setText("修改密码");
		
		mLodPass = (EditText) findViewById(R.id.old_pass);
		mNewPass = (EditText) findViewById(R.id.new_pass);
		mNewPassTwo = (EditText) findViewById(R.id.new_passtwo);
		mPassCode = (EditText) findViewById(R.id.pass_code);
		findViewById(R.id.affirm).setOnClickListener(this);
		mTime = (TextView) findViewById(R.id.time);
		mTime.setOnClickListener(this);
	}
	
	
	/**
	 * @author Administrator 自定义一个倒计时的内部类；
	 */
	class TimeCount extends CountDownTimer {
		public TimeCount(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
		}

		// 当时间到的时候；
		public void onFinish() {
			// TODO Auto-generated method stub
			mTime.setText("重新获取");
			mTime.setClickable(true);
			mTime.setBackgroundResource(R.drawable.button_bg);
		}

		@Override
		public void onTick(long millisUntilFinished) {
			mTime.setClickable(false);
			mTime.setText(millisUntilFinished / 1000 + "秒");
		}
	}


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.time:
			requestCode();
			break;

		case R.id.title_back:
			
			onBackPressed();
			
			break;
		case R.id.affirm:
			commitDate();
			
			break;
		}
	}

	private void commitDate() {
		if(!Misc.notNull(mLodPass.getText().toString())){
			Misc.alert("原始密码不能为空");
		}else if(!Misc.notNull(mNewPass.getText().toString())){
			
		  Misc.alert("请输入新密码");
		}else if(!Misc.notNull(mNewPassTwo.getText().toString())){
			Misc.alert("请确认新密码");
		}else if(mNewPassTwo.getText().toString().length()>6){
			Misc.alert("密码6-12位");
		}
		
		else if(!Misc.notNull(mPassCode.getText().toString())){
			Misc.alert("请输入验证码");
		}else if(!mNewPass.getText().toString().equals(mNewPassTwo.getText().toString())){
			Misc.alert("两次密码不一致");
		}else if(!mPassCode.getText().toString().equals(mCode)){
				Misc.alert("请输入正确的验证码");
		}else {
			HttpRequestUtils.doHttpAlterLoginPasswordCommit(mLodPass.getText().toString(), mNewPassTwo.getText().toString(), mPassCode.getText().toString(), new MyAsyncHttpResponseHandler(getSupportFragmentManager(),getResources()) {
				@Override
				public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
					// TODO Auto-generated method stub
					super.onSuccess(arg0, arg1, arg2);
					
					try {
						JSONObject jsonO = new JSONObject(new String(arg2));
						if(jsonO.optBoolean("status")){
							JSONObject optJSONObject2 = jsonO.optJSONObject("date");
								Misc.alert(optJSONObject2.optString("result"));
								finish();
						}else{
							Misc.alert(jsonO.optString("errMessage"));
						}
						System.out.println(jsonO.toString());
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}
				
				@Override
				public void onFailure(int arg0, Header[] arg1, byte[] arg2,
						Throwable arg3) {
					// TODO Auto-generated method stub
					super.onFailure(arg0, arg1, arg2, arg3);
				}
			});
		}
	}
	
	

	private void requestCode() {
		// TODO Auto-generated method stub
		HttpRequestUtils.doHttpAlterLoginPassword(new MyAsyncHttpResponseHandler(getSupportFragmentManager(),getResources()) {
			
			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				// TODO Auto-generated method stub
				super.onSuccess(arg0, arg1, arg2);
				
				try {//{"data":{"code":663276},"errMessage":"","status":true}
					JSONObject jsonO = new JSONObject(new String(arg2));
					
					if(jsonO.optBoolean("status")){
						
						JSONObject optJSONObject = jsonO.optJSONObject("data");
						
						mCode = optJSONObject.optString("code");
   					  timeCount.start();
						
					}else{
						Misc.alert(jsonO.optString("errMessage"));
					}
					
					
					System.out.println(jsonO.toString());
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			
			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2,
					Throwable arg3) {
				// TODO Auto-generated method stub
				super.onFailure(arg0, arg1, arg2, arg3);
			}
			
		});
	}
	
}
